//
//  AppManager.swift
//  ImGuider X
//
//  Created by 陆涛 on 2018/9/28.
//  Copyright © 2018 imguider. All rights reserved.
//

import UIKit

let  kAllPlayWhenListen =  "kAllPlayWhenListen"

let  KAutomaticPlay = "automaticPlay"


/// app语言
///
/// - chinese: 中文
/// - english: 英文
enum AppLanguage {
    case chinese
    case english
}

class AppManager: NSObject {

    /// 单例对象
    @objc static let share = AppManager()
    
    private var bundle:Bundle = Bundle.main
    
    private override init() {
        super.init()
        
        let _ = currentLanguage()
    }
    
    
    var current_language:String = "en"
    
    
    // 连续播放
    var allPlayWhenListen : Bool {
        
        set{
          
            UserDefaults.standard.set(newValue, forKey: KAutomaticPlay)
            UserDefaults.standard.synchronize()
        }
        
        get{
            
          return UserDefaults.standard.bool(forKey: KAutomaticPlay)
        }
    }
    
    var language:AppLanguage {
        
        get {
            
            return currentLanguage()
        }
        
        set {
            
            setLanguage(newValue)
        }
    }
    
    private func currentLanguage() -> AppLanguage {
        
        guard let array = UserDefaults.standard.array(forKey: "AppleLanguages"),
            let lan = array.first as? String else {
                
                current_language = "en"
                
                return .english
        }
        let lanstr = lan.lowercased()
        
        if lanstr.hasPrefix("zh") {
            
            current_language = "zh-Hans"
            return .chinese
        }
        
        return .english
    }
    
    private func setLanguage(_ lan:AppLanguage) {
        
        var lanstr = "en"
        
        switch lan {
            
        case .chinese:
            
            lanstr = "zh-Hans"
        case .english:
            
            lanstr = "en"
        }
        
        current_language = lanstr
        
        UserDefaults.standard.set([lanstr], forKey: "AppleLanguages")
        UserDefaults.standard.synchronize()
        
        if let path = Bundle.main.path(forResource: lanstr, ofType: "lproj") {
            
            bundle = Bundle(path: path) ?? Bundle.main
        }
        
    }
    
    @objc func localizedString(_ key:String) -> String {
        
        return bundle.localizedString(forKey: key, value: key, table: nil)
    }
    
    //自动播放
    var automaticPlay : Bool {
        
        set{
            
            UserDefaults.standard.set(newValue, forKey: kAllPlayWhenListen)
            UserDefaults.standard.synchronize()
        }
        
        get{
            
            return UserDefaults.standard.bool(forKey: kAllPlayWhenListen)
        }
    }
    var keyWindow: UIWindow? {
        
        let appDelegate = UIApplication.shared.delegate as? AppDelegate
        
        return appDelegate?.window
    }
    
    
    /// 判断是否登录，未登录跳转到登录页面
    ///
    /// - Returns: 是否登录
    func hasLoginOrJumpToLogin() -> Bool {
        
        if nil != UserManager.share.token {
            
            return true
        }
        
        loginPage()
        
        return false
    }
    
    func loginPage(_ jumpScan:Bool = false) {
        
        let sb = UIStoryboard(name: "Login", bundle: nil)
        let loginVC = sb.instantiateViewController(withIdentifier: "LoginVC") as! LoginVC
        loginVC.needScan = jumpScan
        
        let nav = UINavigationController(rootViewController: loginVC)
        
        keyWindow?.rootViewController = nav
    }
    
    func HomePage() {
        
        let cityVC = TripOverViewVC()
        
        let nav = UINavigationController(rootViewController: cityVC)
        
        keyWindow?.rootViewController = nav
    }
    
    
}
